﻿using DotNEToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformToolkit.Navigations
{
    internal class MenuContext
    {
        #region 类变量

        private static log4net.ILog logger = log4net.LogManager.GetLogger("NavigationContext");

        #endregion

        #region 实例变量

        private IContentHost host;

        /// <summary>
        /// 当前正在显示的菜单
        /// </summary>
        private WFMenuItem selectedMenu;

        #endregion

        #region 构造方法

        public MenuContext(string configFile, IContentHost host)
        {
            // 首先解析菜单配置文件
            List<WFMenuItem> menuItems = JSONHelper.ParseFile<List<WFMenuItem>>(configFile);

            this.InitializeContext(menuItems, host);
        }

        public MenuContext(List<WFMenuItem> menuItems, IContentHost host)
        {
            this.InitializeContext(menuItems, host);
        }

        #endregion

        private void InitializeContext(List<WFMenuItem> menuItems, IContentHost host)
        {
            this.host = host;

            // 创建菜单列表
            host.InitializeMenuList(menuItems);

            // 注册事件回调
            host.MenuClicked += this.Host_MenuItemClicked;
        }

        #region 事件处理器

        private void Host_MenuItemClicked(IContentHost host, WFMenuItem menu)
        {
            if (this.selectedMenu == menu)
            {
                // 点击的是同一个菜单，不用触发，直接返回
                return;
            }

            if (menu.Content == null)
            {
                // 此时菜单所对应的内容页面还没创建，创建内容页面的实例
                try
                {
                    menu.Content = ConfigFactory<IContentControl>.CreateInstance(menu.EntryClass);
                    //menu.Content.MenuItem = menu;
                }
                catch (Exception ex)
                {
                    // TODO：创建失败处理，显示一个错误页面
                    logger.ErrorFormat("创建菜单页面异常, {0}, {1}", menu, ex);
                    return;
                }
            }

            // 运行到这里说明页面创建成功
            if (this.selectedMenu != null)
            {
                host.RemoveContent(this.selectedMenu.Content);
                this.ProcessOnUnload(this.selectedMenu.Content);
            }

            // 把页面加到界面上
            host.AddContent(menu.Content);

            // 处理生命周期事件
            if (menu.Content is IContentLifetimeHook)
            {
                IContentLifetimeHook hook = menu.Content as IContentLifetimeHook;

                if (!menu.IsInitialized)
                {
                    hook.OnInitialize();
                    menu.IsInitialized = true;
                }

                hook.OnLoaded();
            }

            // 更新当前选中的菜单
            this.selectedMenu = menu;
        }

        #endregion

        private void ProcessOnUnload(IContentControl content)
        {
            if (content is IContentLifetimeHook)
            {
                IContentLifetimeHook hook = content as IContentLifetimeHook;
                hook.OnUnload();
            }
        }
    }
}
